/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewJInternalFrame.java
 *
 * Created on 22/04/2011, 12:51:00
 */
package asfabdesk.apresentacao;

import asfabdesk.dominio.DepositoFinanceiro;
import asfabdesk.presistencia.DAO;
import javax.swing.JOptionPane;

/**
 *
 * @author Marco Aurelio
 */
public class TelaFinDeposito extends javax.swing.JInternalFrame {

    public DepositoFinanceiro dep = new DepositoFinanceiro();

    /** Creates new form NewJInternalFrame */
    public TelaFinDeposito() {
        initComponents();
    }

    public TelaFinDeposito(DepositoFinanceiro deposito) {
        initComponents();
        dep = deposito;
        povoarCampos();
    }

    public void salvar() {
        String msgSucessoEdt = "Registro Alterado com Sucesso!";
        String msgSucessoCad = "Depósito Cadastrado com Sucesso!";
        String msgErroEdt = "Ops, houve um erro durante a alteração do Registro.";
        String msgErroCad = "Ops, houve um erro durante o cadastro do Depósito.";
        String sucesso;
        String erro;
        if (verificarCampos()) {
            if (verificarDup()) {
                povoarDeposito();
                if (dep.getId() != null) {
                    sucesso = msgSucessoEdt;
                    erro = msgErroEdt;
                } else {
                    sucesso = msgSucessoCad;
                    erro = msgErroCad;
                }
                try {
                    dep = new DAO().salvar(dep);
                    JOptionPane.showMessageDialog(null, sucesso, "Sucesso!", 1);
                    povoarCampos();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, erro, "Ops,erro!", 0);
                }
            }
        }
    }

    public boolean verificarCampos() {
        boolean retorno = true;
        if (txtDescricao.getText().equals("")) {
            retorno = false;
        }
        if (retorno == false) {
            JOptionPane.showMessageDialog(null, "Ops, você não preencheu o campo descrição!", "Campos Obrigatórios.", 2);
        }
        return retorno;
    }

    public boolean verificarDup() {
        boolean r = true;
        if (new DAO().localizar(DepositoFinanceiro.class, "depositoPorNome", txtDescricao.getText().toUpperCase()) != null) {
            if (dep.getDescricao()!= null && dep.getDescricao().equals(txtDescricao.getText().toUpperCase())) {
            } else {
                r = false;
            }
        }
        if (r == false) {
            JOptionPane.showMessageDialog(null, "Ops, já existe um Depósito com este Nome!", "Ops.", 2);
        }
        return r;
    }

    public void povoarDeposito() {
        dep.setDescricao(txtDescricao.getText().toUpperCase());
        dep.setRealizaEntrada(chkEntradas.isSelected());
        dep.setRealizaSaida(chkSaidas.isSelected());
        dep.setRealizaTransferencia(chkTransferencias.isSelected());
        dep.setUsuarioCadastrante(TelaLogin.usuario);
    }

    public void povoarCampos() {
        txtDescricao.setText(dep.getDescricao());
        txtFundo.setText(String.valueOf(dep.getFundoMonetario()));
        txtUsuario.setText(dep.getUsuarioCadastrante().getNome());
        if (dep.isStatus()) {
            txtStatus.setText("Ativo");
        } else {
            txtStatus.setText("Inativo");
        }
        chkEntradas.setSelected(dep.isRealizaEntrada());
        chkSaidas.setSelected(dep.isRealizaSaida());
        chkTransferencias.setSelected(dep.isRealizaTransferencia());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSeparator5 = new javax.swing.JSeparator();
        jSeparator6 = new javax.swing.JSeparator();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtDescricao = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtStatus = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtFundo = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        chkSaidas = new javax.swing.JCheckBox();
        chkEntradas = new javax.swing.JCheckBox();
        chkTransferencias = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        jSeparator7 = new javax.swing.JSeparator();
        jToolBar1 = new javax.swing.JToolBar();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        btnSalvar = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        btnAtivaInativa = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnRemover = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();

        jSeparator5.setName("jSeparator5"); // NOI18N

        jSeparator6.setName("jSeparator6"); // NOI18N

        setClosable(true);
        setIconifiable(true);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(asfabdesk.apresentacao.AsfabDeskApp.class).getContext().getResourceMap(TelaFinDeposito.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setOpaque(true);

        jPanel4.setName("jPanel4"); // NOI18N

        jPanel3.setName("jPanel3"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        txtDescricao.setText(resourceMap.getString("txtDescricao.text")); // NOI18N
        txtDescricao.setName("txtDescricao"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtStatus.setEditable(false);
        txtStatus.setText(resourceMap.getString("txtStatus.text")); // NOI18N
        txtStatus.setName("txtStatus"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txtFundo.setEditable(false);
        txtFundo.setText(resourceMap.getString("txtFundo.text")); // NOI18N
        txtFundo.setName("txtFundo"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel2.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jPanel2.border.titleFont"), resourceMap.getColor("jPanel2.border.titleColor"))); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setOpaque(false);

        chkSaidas.setText(resourceMap.getString("chkSaidas.text")); // NOI18N
        chkSaidas.setName("chkSaidas"); // NOI18N
        chkSaidas.setOpaque(false);

        chkEntradas.setText(resourceMap.getString("chkEntradas.text")); // NOI18N
        chkEntradas.setName("chkEntradas"); // NOI18N
        chkEntradas.setOpaque(false);

        chkTransferencias.setText(resourceMap.getString("chkTransferencias.text")); // NOI18N
        chkTransferencias.setName("chkTransferencias"); // NOI18N
        chkTransferencias.setOpaque(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkSaidas)
                    .addComponent(chkEntradas)
                    .addComponent(chkTransferencias))
                .addContainerGap(249, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(chkSaidas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkEntradas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkTransferencias))
        );

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        txtUsuario.setEditable(false);
        txtUsuario.setName("txtUsuario"); // NOI18N

        jSeparator7.setName("jSeparator7"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, 296, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDescricao, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtFundo, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtFundo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(11, 11, 11)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addComponent(jSeparator7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8))
        );

        jToolBar1.setBorder(null);
        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jToolBar1.setName("jToolBar1"); // NOI18N
        jToolBar1.setOpaque(false);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar1.add(jSeparator1);

        btnSalvar.setFont(resourceMap.getFont("btnSalvar.font")); // NOI18N
        btnSalvar.setForeground(resourceMap.getColor("btnSalvar.foreground")); // NOI18N
        btnSalvar.setIcon(resourceMap.getIcon("btnSalvar.icon")); // NOI18N
        btnSalvar.setText(resourceMap.getString("btnSalvar.text")); // NOI18N
        btnSalvar.setToolTipText(resourceMap.getString("btnSalvar.toolTipText")); // NOI18N
        btnSalvar.setName("btnSalvar"); // NOI18N
        btnSalvar.setOpaque(false);
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSalvar);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar1.add(jSeparator2);

        btnAtivaInativa.setFont(resourceMap.getFont("btnAtivaInativa.font")); // NOI18N
        btnAtivaInativa.setForeground(resourceMap.getColor("btnAtivaInativa.foreground")); // NOI18N
        btnAtivaInativa.setIcon(resourceMap.getIcon("btnAtivaInativa.icon")); // NOI18N
        btnAtivaInativa.setText(resourceMap.getString("btnAtivaInativa.text")); // NOI18N
        btnAtivaInativa.setToolTipText(resourceMap.getString("btnAtivaInativa.toolTipText")); // NOI18N
        btnAtivaInativa.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        btnAtivaInativa.setName("btnAtivaInativa"); // NOI18N
        btnAtivaInativa.setOpaque(false);
        jToolBar1.add(btnAtivaInativa);

        jSeparator3.setName("jSeparator3"); // NOI18N
        jToolBar1.add(jSeparator3);

        btnRemover.setFont(resourceMap.getFont("btnRemover.font")); // NOI18N
        btnRemover.setForeground(resourceMap.getColor("btnRemover.foreground")); // NOI18N
        btnRemover.setIcon(resourceMap.getIcon("btnRemover.icon")); // NOI18N
        btnRemover.setText(resourceMap.getString("btnRemover.text")); // NOI18N
        btnRemover.setToolTipText(resourceMap.getString("btnRemover.toolTipText")); // NOI18N
        btnRemover.setName("btnRemover"); // NOI18N
        btnRemover.setOpaque(false);
        jToolBar1.add(btnRemover);

        jSeparator4.setName("jSeparator4"); // NOI18N
        jToolBar1.add(jSeparator4);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        salvar();
    }//GEN-LAST:event_btnSalvarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAtivaInativa;
    private javax.swing.JButton btnRemover;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JCheckBox chkEntradas;
    private javax.swing.JCheckBox chkSaidas;
    private javax.swing.JCheckBox chkTransferencias;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JSeparator jSeparator7;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField txtDescricao;
    private javax.swing.JTextField txtFundo;
    private javax.swing.JTextField txtStatus;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
